package com.dq.redis.xiaoxi.realize;


import com.dq.redis.jedispool.JedisUtils;
import com.dq.service.messsge.SubscribeMessageService;
import com.dq.utils.SpringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;

/**
 * (监听者)
 *      获取redis中消息并进行处理
 * @author 凡
 */
public class RedisSubscriberRealize implements MessageListener {


    /**
     * 处理消息逻辑
     *
     * @param message 完整的消息(包括频道的信息, 以及消息的具体内容) publish
     * @param pattern 获取的频道信息
     */
    @Override
    public void onMessage(Message message, byte[] pattern) {
        // 获取 频道 名称
        String channelName = new String(message.getChannel());
        // 获取 信息
        String messageKey = new String(message.getBody());

        switch(channelName)
        {
            case "__keyevent@0__:expired":
                System.out.println(" >>>> 获取 失效 key: " + messageKey);
                SpringUtil.getBean(SubscribeMessageService.class).subscribeRedis(messageKey);
                break;
            case "":
                System.out.println("--------------");
                break;
            default:
                System.out.println("从channel(频道): " + channelName + ", 获取消息内容: " + messageKey);
                break;
        }

    }

}
